//引入样式
require('../../assets/css/normalize.css')
require('../../assets/css/reset.css')
require('../../assets/fonts/iconfont.css')
require('./register.less')

//引入模块
const CaptchaMini = require('captcha-mini')
const dom = require('../../utils/dom.js')
const axios =require('axios')


    document.addEventListener('DOMContentLoaded', function () {
        //实例化验证码
        let captcha = new CaptchaMini();
        let codeInit = '';
        captcha.draw(document.querySelector('#captcha'), r => {
            codeInit = r.toLowerCase()
        });


        //点击注册账号时开始验证(表单验证)
        //获取错误提示文字
        let error = dom.get(".reg-prompt-fotter");
        let btn = dom.get(".register")
        btn.addEventListener("click", function () {
            //手机号验证
            let tel = dom.get(".tel").value;
            const reg = /^1[3456789]\d{9}$/i
            if (reg.test(tel)) {
                error.textContent = ``
            } else {
                error.textContent = `手机号格式错误`
                return;
            }

            //验证码验证
            let code = dom.get('.code').value.toLowerCase();
            if (code === codeInit) {
                error.textContent = ``
            } else {
                error.textContent = `验证码错误`
                return;
            }

            //密码验证
            let pwd = dom.get('.pwd').value;
            if (pwd.length >= 6 && pwd.length <= 10) {
                error.textContent = ``
            } else {
                error.textContent = '密码必须写6-10位数之间';
                return;
            }

            //确认密码验证
            let pwd2 = dom.get('.pwd2').value
            if (pwd === pwd2) {
                error.textContent = ``
            } else {
                error.textContent = `两次密码不一致`
                return;
            }

            //验证通过发送请求
            axios.post('http://fitness.h5.itsource.cn:3701/api/user/register',{
                account:tel,
                password:pwd2,
            }).then(res=>{
                if(res.data.errno===0){
                    location.href='login.html';
                    alert('注册成功')
                }else{
                    alert('注册失败'+res.data.message)
                }
            })


        })




    })
